# -*- coding=utf-8

import logging
import os
import sys
import time
from pprint import pprint

from qcloud_cos import CosConfig, CosServiceError
from qcloud_cos import CosS3Client
from qcloud_cos.cos_threadpool import SimpleThreadPool

# 正常情况日志级别使用 INFO，需要定位时可以修改为 DEBUG，此时 SDK 会打印和服务端的通信信息
logging.basicConfig(level=logging.INFO, stream=sys.stdout)

# 1. 设置用户属性, 包括 secret_id, secret_key, region等。Appid 已在 CosConfig 中移除，请在参数 Bucket 中带上 Appid。Bucket 由 BucketName-Appid 组成
# secret_id = 'AKIDyyFnIm7NYzLdq1ACINNBCNRQKDB3t6Xz'  # 用户的 SecretId，建议使用子账号密钥，授权遵循最小权限指引，降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140
# secret_key = 'lxHgE7ydAoOrPSWes7LUbp3JXj5d9bu8'  # 用户的 SecretKey，建议使用子账号密钥，授权遵循最小权限指引，降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140
# bucket = 'context-1257245862'

secret_id = 'AKIDoY8VgQZ3we9RceATIwPfidOyMbeVr5pO'  # 用户的 SecretId，建议使用子账号密钥，授权遵循最小权限指引，降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140
secret_key = 'TkqmZDgpqfVr8HF1aw9AyiGwVs57xa5R'  # 用户的 SecretKey，建议使用子账号密钥，授权遵循最小权限指引，降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140
bucket = 'demo-1251808174'
queue_id = 'p75e7d74b6ef14ae5936b380fcd9ec4ff'

region = 'ap-chengdu'  # 替换为用户的 region，已创建桶归属的 region 可以在控制台查看，https://console.cloud.tencent.com/cos5/bucket
# COS 支持的所有 region 列表参见 https://cloud.tencent.com/document/product/436/6224
# token = '6jmwo5E6ibUgBjQZRajnwmNNv2HW59ba88615ed1bca62a27320efa548b14320bEcSKHQlM_7imF128QIzcxC_bggVEw9t8d4zl6tbk8YgNBv1--wwHm6BVQp3B1ziAHSdXtdzJp9cqZU3XjK44PX1fd1e7j3CbCEdQKCfvVUMj6uFxhCaWYLFUC0ZwTvTcm8-4zGie1l2F5Q9V5Aepb4RRIf12kg-KD6mtgA8AaWOBnjREvoxhv1mzgX3LtZ2GPxa8Dn6o_LnKmLwUUA-6M57FqBh4kt3rxNtTbkJExYRHCxhJNTG9tXxwnw2m9AI6JVQsKiEPZILW8-KqmOIdr3VSHtpRIywoOOMfsLHK-Y5V1kSlFuh3hivP7Y_Nnj44OrhatuPTI0Pf7nBZh_1B4dBNrMR2JNLA4AbhDMikpqaCRPm9nJB5HjUjU6fK82XrLKUr_dm9cBAdvko141oo_c8y-o1F0JdBCplwUSynh-4FHE9VZIdBJxB08WHmHBkWeuBU5YFwC48Q1kH7j4kOd8h2OTtH63EA9dEwwclO4rkzUW7TOUGzQhy_QL4oiyf3ZltOP7alj1mND9zrEGvUGIQdFxj5uzUQelMfHoLUi5HvMGhCpU2mnGY4OJnKbJE-nSC8jsboOpA_5DG4o8XDa6FfuLKyKiV1YcSGuLpx33WET0XRR4dOUPQQYrQ-O_dC3TARB-ohPHJZnvYyjt4XAFJwuqM_M2TjFM5plTVVBb-Op9Ylin0WlwcAPWjDXkRdQ_i6EBlHb6l7vOtmFx_wehU_THaScUc1waHC8Gr7OZs'               # 如果使用永久密钥不需要填入 token，如果使用临时密钥需要填入，临时密钥生成和使用指引参见 https://cloud.tencent.com/document/product/436/14048
token = None
scheme = 'https'  # 指定使用 http/https 协议来访问 COS，默认为 https，可不填

config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Scheme=scheme)
client = CosS3Client(config)
#
# response = client.list_buckets()
# print('buckets:', response)
{'Bucket': 'demo-1251808174',
 'InputObject': 'media/edoc/common/2024-10/-570615888/2025金版教程_数学_第八章_第3讲_作业40.zip',
 'OutputBucket': 'demo-1251808174',
 'OutputRegion': 'ap-chengdu',
 'FileUncompressConfig': {'Prefix': 'media/edoc/common/2024-10/3369/', 'PrefixReplaced': '0'}}


{'Bucket': 'demo-1251808174',
 'InputObject': 'media/edoc/common/2024-10/-570629607/abc.zip',
 'OutputBucket': 'demo-1251808174',
 'OutputRegion': 'ap-chengdu',
 'FileUncompressConfig': {'Prefix': 'media/edoc/common/2024-10/3372/', 'PrefixReplaced': '0'}}


# media/edoc/common/2024-05/-44640109/page_2_6.pdf
source = "media/edoc/common/2024-10/-570629607/abc.zip"
resp = client.ci_create_file_uncompress_job(
    Bucket=bucket,
    InputObject=source,
    OutputBucket=bucket,
    OutputRegion=region,
    # FileUncompressConfig={'Prefix': os.path.dirname(self.source) + '/', 'PrefixReplaced': '0'},  # 文件解压配置详情
    FileUncompressConfig={'Prefix': f'/media/edoc/common/2024-10/3372/',
                          'PrefixReplaced': '0'},  # 文件解压配置详情
)
job = resp.get('JobsDetail')

print(job)
print(job['JobId'])

time.sleep(2)
resp = client.ci_get_doc_job(
    Bucket=bucket,
    JobID=job['JobId']
    # JobID='f533c6fa8901811efbbfed7db5f971b84'
)
pprint(resp)

